"""LLM功能测试示例"""

import asyncio
import sys
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

from llm.llm_manager import llm_manager
from agents.core.code_generator import CodeGeneratorAgent
from agents.base.agent_state import Task


async def test_llm_managers():
    """测试LLM管理器"""
    print("=== 测试LLM管理器 ===")
    
    # 测试百炼平台
    print("\n1. 测试百炼平台基础聊天:")
    try:
        messages = [
            {"role": "system", "content": "你是一个有用的助手。"},
            {"role": "user", "content": "你好，请简单介绍一下你自己"}
        ]
        
        response = llm_manager.chat_completion("bailian", messages)
        print(f"回复: {response.choices[0].message.content}")
        
    except Exception as e:
        print(f"百炼平台测试失败: {e}")
    
    # 测试QwQ深度思考
    print("\n2. 测试QwQ深度思考:")
    try:
        messages = [{"role": "user", "content": "请解释一下什么是人工智能"}]
        
        print("思考过程:")
        for chunk in llm_manager.stream_thinking_chat("bailian_qwq", messages):
            if chunk["type"] == "reasoning":
                print(chunk["content"], end="", flush=True)
            elif chunk["type"] == "answer_start":
                print("\n\n回答:")
                print(chunk["content"], end="", flush=True)
            elif chunk["type"] == "answer":
                print(chunk["content"], end="", flush=True)
        
        print("\n")
        
    except Exception as e:
        print(f"QwQ深度思考测试失败: {e}")


async def test_code_generator():
    """测试代码生成智能体"""
    print("\n=== 测试代码生成智能体 ===")
    
    try:
        # 创建代码生成智能体
        agent = CodeGeneratorAgent()
        await agent.initialize()
        
        # 创建代码生成任务
        task = Task(
            id="test_001",
            name="生成Python函数",
            description="生成一个计算斐波那契数列的函数",
            metadata={
                "type": "generate_code",
                "requirements": {
                    "language": "python",
                    "functionality": "计算斐波那契数列的第n项"
                }
            }
        )
        
        print("正在生成代码...")
        result = await agent.process_task(task)
        
        if result["status"] == "success":
            print("代码生成成功！")
            print(f"语言: {result['language']}")
            print("生成的代码:")
            print("-" * 50)
            print(result["code"])
            print("-" * 50)
        else:
            print(f"代码生成失败: {result.get('error', '未知错误')}")
            
    except Exception as e:
        print(f"代码生成智能体测试失败: {e}")


async def main():
    """主函数"""
    print("AI Agent Test - LLM功能测试")
    print("=" * 50)
    
    # 测试LLM管理器
    await test_llm_managers()
    
    # 测试代码生成智能体
    await test_code_generator()
    
    print("\n测试完成！")


if __name__ == "__main__":
    asyncio.run(main())
